package com.support.config;

import com.youshang520i.model.SystemUser;
import com.youshang520i.thread.SystemUserThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author youshang
 */
public class MyHandlerInterceptor implements HandlerInterceptor {

    /**
     * 1
     * 找方法执行之前调用
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (!"".equals(token) && null != token) {
            SystemUser systemUser = new SystemUser();
            systemUser.setUserName("youshang520i");
            SystemUserThreadLocal.set(systemUser);
        }
        return true;
    }

    /**
     * 2
     * 在方法执行时调用
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println();
    }

    /**
     * 3
     * 在方法执行完的时候调用
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        SystemUserThreadLocal.remove();
    }
}
